package com.tbit.uqbike.webmanager.controller;

import com.tbit.common.entity.view.Result;
import com.tbit.uqbike.object.pojo.statistics.ExportViolationLog;
import com.tbit.uqbike.object.pojo.statistics.ViolationLog;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.uqbike.service.business.ViolationLogService;
import com.tbit.uqbike.webmanager.util.ServletUtils;
import com.tbit.uqbike.webmanager.util.excel.ExportExcelUtil;
import com.tbit.uqbike.webmanager.util.excel.ExportExcelWrapper;
import com.tbit.utils.DateTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.Date;
import java.util.List;

/**
 * @Author: chen
 * @Description: 运维举报
 * @Date: 创建于 16:24 2020/3/16
 */
@Validated
@RestController
@RequestMapping("/violationLog")
public class ViolationLogController {
    @Autowired
    private ViolationLogService violationLogService;

    /**
     * 分页查询详细
     */
    @RequestMapping("/queryPage")
    public JsonResult queryPage(Integer accountId, Integer violationState, Integer reportUserId, String userPhone, String userCode,
                                Integer violationType, Long startTime, Long endTime, @RequestParam(defaultValue = "1") Integer pageNO, @RequestParam(defaultValue = "10") Integer rowCount) {

        List<ViolationLog> list = violationLogService.queryPage(accountId, violationState, reportUserId, userPhone, userCode, violationType, startTime, endTime, pageNO, rowCount);
        Integer total = violationLogService.queryCount(accountId, violationState, reportUserId, userPhone, userCode, violationType, startTime, endTime);
        return JsonResult.ok(list, total);
    }

    /**
     * 导出
     */
    @RequestMapping("/violationLogExport")
    public void export(Integer accountId, Integer violationState, Integer reportUserId, String userPhone, String userCode,
                       Integer violationType, Long startTime, Long endTime, HttpServletResponse response) {
        List<ExportViolationLog> exports = violationLogService.export(accountId, violationState, reportUserId, userPhone, userCode, violationType, startTime, endTime);
        ExportExcelWrapper<ExportViolationLog> util = new ExportExcelWrapper<>();
        String filename = "运维举报" + DateTimeUtil.getNowTimeYMD();
        String[] columnNames = {"车辆编号", "用户名称", "用户手机号码", "举报人名称", "举报类型", "举报时间"/*,"举报位置"*/, "举报描述", "状态", "处理人名称", "处理时间", "处理结果", "处理描述"};
        util.exportExcel(filename, filename, columnNames, exports, response, ExportExcelUtil.EXCEL_FILE_2003);
    }

    /**
     * 处理举报记录
     *
     * @param violationLogId 举报记录id
     * @param handleResult   处理结果 0:驳回 1:通过
     * @param handleDesc     处理内容 如果是通过并回复,这里填回复内容。如果是驳回，这里填驳回原因
     * @param type           1:小程序回复用户 2:拉黑用户
     * @param blockTime      拉黑时间(年-月-日)
     * @param photosUrl      拉黑照片 上传的照片的url，多个使用,隔开
     */
    @RequestMapping("/handle")
    public Result<String> handleNew(@NotNull Integer violationLogId, @NotNull Integer handleResult,
                                    @Size(max = 200, message = "回复内容限制0-200汉字") String handleDesc, Integer type,
                                    Date blockTime, String photosUrl) {
        LoginInfo login = BaseController.getLogin(ServletUtils.getRequest());
        if (handleDesc == null) {
            handleDesc = "";
        }

        violationLogService.handle(login, violationLogId, handleResult, handleDesc, type, blockTime, photosUrl);

        return Result.success();
    }
}
